<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/28
 * Time: 17:06
 */
namespace App\Service\Api;

use App\Http\Model\MongoDB\LiveList;
use App\Http\Model\Mysql\ProductModel;
use App\Service\Live\AlterService;
use App\Service\Live\ClientService;
use App\Service\Live\OperateService;
use App\Utils\WechatUtil;
use Carbon\Carbon;

class LiveService extends BaseService
{
    /**
     * 直播列表
     * @param int $page
     * @return array
     */
    public function liveList(int $page = 1)
    {
        /**
         * status == 0  直播预告
         * status == 1 || status == 2   直播中
         * status == 3  直播回放
         */
        $service=   new OperateService();
        $client =   new ClientService();
        $alter  =   new AlterService();
        $list   =   LiveList::query()->limit(self::PAGE_LIMIT)->offset(($page - 1) * self::PAGE_LIMIT)->orderBy('PlanedStartTime', 'desc')->select(['Statistics', 'Id', 'Title', 'RTMPPullUrl', 'cc', 'StartTime', 'EndTime', 'HostId', 'HostName', 'GoodNum', 'PushAddress', 'PushUrl', 'MainImg', 'Status', 'ProductIds'])
                    ->get()->each(function (&$item) use($service, $client, $alter){
                        $item->StartTime    =   Carbon::createFromTimestamp($item->StartTime)->toDateTimeString();
                        $item->PlanedStartTime  =   Carbon::createFromTimestamp($item->PlanedStartTime)->toDateTimeString();
                        $item->EndTime      =   Carbon::createFromTimestamp($item->EndTime)->toDateTimeString();
                        $redis_array        =   $service->goodsGet($item->Id);
                        $mongo_array        =   $item->ProductIds;
                        $goods_array        =   array_merge($redis_array ?? [], $mongo_array ?? []);
                        //商品总列表
                        $item->GoodList     =   ProductModel::query()->sell()->whereIn('id', $goods_array)->select('id', 'unique_id', 'name', 'sell_price', 'cover_pic')->get();
                        //商品数量
                        $item->GoodNum      =   count($item->GoodList);
                        //直播间当前观看人数
                        $item->Views        =   $client->getUserWatch($item->Id) ?? 0;
                        //直播间关注人数
                        $item->TotalFocus   =   $alter->totalFocus($item->Id) ?? 0;
                        //直播间查看回放人数
                        $item->RollBack     =   100;
                    });
        return [true, $list->isEmpty() ? [] : $list->toArray()];
    }

    /**
     * 查看直播间详情
     * @param int $room_id
     * @return array
     */
    public function liveInfo(int $room_id)
    {
        $detail =   LiveList::where(['Id' => $room_id])->select(['Id', 'Title', 'RTMPPullUrl', 'PlanedStartTime', 'StartTime', 'EndTime', 'HostId', 'HostName', 'GoodNum', 'PushAddress', 'PushUrl', 'MainImg', 'Status'])->first();
        if (empty($detail)) {
            return [false, '直播间不存在'];
        } else {
            /*if ($detail['Status'] != 1 || $detail['Status'] != 2) {
                return [false, '直播间未在播放'];
            }*/
            $detail['StartTime']        =   Carbon::createFromTimestamp($detail['StartTime'])->toDateTimeString();
            $detail['EndTime']          =   Carbon::createFromTimestamp($detail['EndTime'])->toDateTimeString();
            $detail['PlanedStartTime']  =   Carbon::createFromTimestamp($detail['PlanedStartTime'])->toDateTimeString();
            return [true, $detail];
        }
    }

    /**
     * 直播间海报地址生成
     * @param int $room_id
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function livePoster(int $room_id)
    {
        $user_info  =   request()->post('api_user');
        [$flag, $result]    =   WechatUtil::getUnlimitedCode(['scene' => 'live'], $user_info['user_id']);    //, 'page' => '/pages/live/index?user_id=' . $user_info['unique_id'] . '&live_id=' . $room_id
        if ($flag && isset($result['img_path'])) {
            return [true, [
                'user_info' =>  $user_info,
                'code_img'  =>  $result['img_path'],
            ]];
        } else {
            return [false, '二维码生成失败'];
        }
    }

}
